package top.huntwolf.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.pojo.storage.BookStorage;
import top.huntwolf.admin.pojo.storage.TransferVo;
import top.huntwolf.admin.service.BookStorageService;

import java.util.List;

@Api(description = "库存控制类")
@RestController
@CrossOrigin
@RequestMapping(value = "/storage")
public class BookStorageController {

    @Autowired
    private BookStorageService storageService;

    @RequiresPermissions(value = "storage:add")
    @PostMapping()
    @ApiOperation("添加新库存")
    public JsonResult addBookStorage(@RequestBody List<BookStorage> storages){
        storageService.addBookStorage(storages);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @RequiresPermissions(value = "storage:transfer")
    @PutMapping("/transfer")
    @ApiOperation("转移库存")
    public JsonResult transferStorage(@RequestBody TransferVo transferVo){
        storageService.transferBook(transferVo);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @RequiresPermissions(value = "storage:modify")
    @PutMapping()
    @ApiOperation("修改库存信息")
    public JsonResult modifyStorage(@RequestBody BookStorage bookStorage){
        storageService.modifyStorage(bookStorage);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @RequiresPermissions(value = "storage:find")
    @GetMapping("/isbn/{isbn}")
    @ApiOperation("通过isbn查询库存")
    public JsonResult<List<BookStorage>> queryBookStorageByIsbn(@PathVariable String isbn){
        return JsonResult.success(storageService.searchStorageByIsbn(isbn));
    }

    @RequiresPermissions(value = "storage:remove")
    @DeleteMapping("/id/{id}")
    @ApiOperation("删除库存")
    public JsonResult deleteBookStorage(@PathVariable Integer id){
        storageService.deleteStorage(id);
        return JsonResult.success(CodeMsg.SUCCESS);
    }


}
